/**
 * @author Ahammer
 * @date   2009 12
 * @update 2011 01 added 16 bit support
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package op;

import java.awt.Shape;
import java.awt.geom.Area;
import java.awt.image.renderable.ParameterBlock;
import java.util.Iterator;
import java.util.Vector;

import javax.media.jai.JAI;
import javax.media.jai.ParameterBlockJAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.ROI;
import javax.media.jai.ROIShape;
import javax.media.jai.TiledImage;
import main.Board;
import main.IqmTools;


/**
 * This is the main image processing class
 * There exist two approaches:
 * A user defined JAI operator is just called 
 * or:
 * The actual processing is implemented in this class 
 */
public class IqmROISegmentOperator{ 


	public IqmROISegmentOperator() {
		//WARNING: Don't declare fields here
		//Fields declared here aren't thread safe!   
	}
  
  
	/**
	 * 
	 * @param ParametrBlockJAI pb
	 */
	public Object run(ParameterBlockJAI pbJAI){
		//RenderingHints rh = IqmTools.getDefaultTileCacheRenderingHints();	    
		//Object ob = JAI.create("IqmROISegment".toLowerCase(), pbJAI, null);	  
		
		Object ob = null;
	    PlanarImage pi = (PlanarImage) pbJAI.getSource(0);
	    
	  	int method   = pbJAI.getIntParameter("Method");
	  	int binarize = pbJAI.getIntParameter("Binarize");
	  	
	    String type = IqmTools.getImgTyp(pi);
		double typeGreyMax = IqmTools.getImgTypeGreyMax(pi);  	
		String imgName  = (String) pi.getProperty("image_name");
	  	String fileName = (String) pi.getProperty("file_name");
	  	 

	  	ROIShape rs = IqmTools.getLookDisplayJAI().getCurrROIShape();
	  	
	  	if (rs == null) {			
			Board.appendTexln("IqmROISegmentOperator: Currrent ROI is not defined and null!");
			Board.appendTexln("IqmROISegmentOperator: ROI processing not possible");
			return pi;
		}
		else{
			Board.appendTexln("IqmROISegmentOperator: Current ROI is defined and valid");		
		}
	  	if (method == 0){
	  		//do nothing, already set
	  		//rs = IqmTools.getLookDisplayJAI().getCurrROIShape();
	  	}
	  	if (method == 1){
	  		Area area = new Area(rs.getAsShape());  	  
	  		Vector<ROIShape> shapeVector = IqmTools.getLookDisplayJAI().getROIShapeVector();
		    Iterator itr = shapeVector.iterator();
		    while( itr.hasNext() ) {
		    	ROIShape roiShape = (ROIShape)itr.next();
		    	if (roiShape != null){
			       area.add(new Area(roiShape.getAsShape()));
		    	}
		    }
		    rs = new ROIShape(area);
	  	}
	
		 	
	  	if (binarize == 0){
		  	TiledImage ti = new TiledImage(pi.getMinX(), pi.getMinY(), pi.getWidth(), pi.getHeight(),
		  								   pi.getTileGridXOffset(), pi.getTileGridYOffset(),
		  								   pi.getSampleModel(), pi.getColorModel());	  	
	  		try {
				ti.setData(pi.copyData(), rs);
			} catch (NullPointerException e) {
				Board.appendTexln("IqmROISegmentOperator: Segmentation with ROI is not possible, perhaps the ROI is a line");
				//e.printStackTrace();
			}	
			ob = ti;
	  	}
	  	if (binarize == 1){
	  		ParameterBlock pb = new ParameterBlock();	
	  		pb.removeSources();
	  		pb.removeParameters();
	  		pb.addSource(pi);
	  		double[] c = new double[1];
	  		c[0] = typeGreyMax;
	  		pb.add(c);
	  		pi = JAI.create("AddConst", pb, null);
	  		
	  		if (pi.getNumBands() == 3){ //make single band image
			    double[][] m = {{ 1.0d/3.0d, 1.0d/3.0d, 1.0d/3.0d, 0.0d }};
			    pi = JAI.create( "bandcombine", new ParameterBlock().addSource(pi).add(m), null);
	  		}	  
		    TiledImage ti = new TiledImage(pi.getMinX(), pi.getMinY(), pi.getWidth(), pi.getHeight(),
	  				   					   pi.getTileGridXOffset(), pi.getTileGridYOffset(),
	  				   					   pi.getSampleModel(), pi.getColorModel());	 
	  		ti.setData(pi.copyData(), rs);	
			ob = ti;
	  	}
	  	((PlanarImage)ob).setProperty("image_name", imgName);
	  	((PlanarImage)ob).setProperty("file_name", fileName);
	    return ob;
	}
}
